package com.wangjin.rockmqapi.quickstart;

/*
 * @Author Wangjin
 * @Date 2019-02-12
 * @Email wangjin4901@vip.qq.com
 */

import com.wangjin.rockmqapi.constants.Const;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;

public class Producer {
    public static void main(String[] args) throws MQClientException, RemotingException, InterruptedException, MQBrokerException {
        DefaultMQProducer producer = new DefaultMQProducer("test_quick_producer_name");
        producer.setNamesrvAddr(Const.NAMESRV_ADD);
        producer.start();
        /*
            topic: 主题
            tag: 标签 用于消息过滤
            keys: 用户自定义的key,唯一标识
            body: 消息内容实体(一定是byte[])
         */
        for (int i = 0 ; i < 5 ; i++) {
            // 1.创建消息
            Message message = new Message("test_quick_topic", "TagA", "key" + i, ("Hello Rocket" + i).getBytes());
            // 2. 发送消息
            SendResult sendResult =  producer.send(message);
            System.err.println("消息发出:" + sendResult);
        }
        producer.shutdown();
    }
}
